// [[ OnflowGenerator File UUID: 9e8074965fdb4c26ae7699f4297bc925  ]]
package com.itdct.onflow.system.role.service

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.role.entity.SysRoleComponentEntity
import com.itdct.onflow.system.role.mapper.SysRoleComponentMapper
import com.itdct.onflow.system.role.qo.SysRoleComponentEditQo
import com.itdct.onflow.system.role.qo.SysRoleComponentSearchQo
import com.itdct.onflow.system.role.vo.SysRoleComponentDetailVo
import com.itdct.onflow.system.role.vo.SysRoleComponentListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-17 16:36:22
 * @version 1.0
 * @description 角色组件关联服务
 */
@Service
class SysRoleComponentService(
    val sysRoleComponentMapper: SysRoleComponentMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysRoleComponentEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysRoleComponentEntity::class.java)
        val id = sysRoleComponentMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysRoleComponentEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysRoleComponentMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysRoleComponentMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysRoleComponentEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysRoleComponentMapper.selectByIdsEx(ids)
        sysRoleComponentMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysRoleComponentSearchQo, context: Context): RespVo<SysRoleComponentDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysRoleComponentMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("角色组件关联", id))
        return success(vo.dict())
    }

    fun page(qo: SysRoleComponentSearchQo, context: Context): RespPageVo<SysRoleComponentListVo> {
        startPage(context)
        val pageData = sysRoleComponentMapper.findByList(qo)
        return successPage(pageData.dict())
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectRoleIdList(qo: SysRoleComponentSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleComponentMapper.selectRoleIdList(qo)
        return success(vos)
    }

    fun selectComponentIdList(qo: SysRoleComponentSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleComponentMapper.selectComponentIdList(qo)
        return success(vos)
    }

    fun selectMenuIdList(qo: SysRoleComponentSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysRoleComponentMapper.selectMenuIdList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]

    fun saveRoleRel(eo: SysRoleComponentEditQo, context: Context): RespVo<Unit> {
        val roleId = eo.roleId ?: fail(ReturnCode.LACK_PARAM, "请输入角色id")
        val menuId = eo.menuId ?: fail(ReturnCode.LACK_PARAM, "请输入菜单id")
        val platformType = eo.platformType ?: fail(ReturnCode.LACK_PARAM, "请输入平台类型")
        val queryWrapper = KtQueryWrapper(SysRoleComponentEntity::class.java)
        queryWrapper.eq(SysRoleComponentEntity::roleId, roleId)
            .eq(SysRoleComponentEntity::menuId, menuId)
        sysRoleComponentMapper.delete(queryWrapper)
        val componentIdList = eo.componentIdList
        if (componentIdList != null) {
            val sysRoleComponentEntities = componentIdList.map {
                val sysRoleComponentEntity = SysRoleComponentEntity()
                sysRoleComponentEntity.roleId = roleId
                sysRoleComponentEntity.menuId = menuId
                sysRoleComponentEntity.componentId = it
                sysRoleComponentEntity.platformType = platformType
                sysRoleComponentEntity
            }
            sysRoleComponentMapper.saveBatch(sysRoleComponentEntities)
        }
        return success()
    }
}